/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.information;

import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.goldshop.information.service.InformationService;
import com.kmcsoft.goldshop.model.master.Currency;
import com.kmcsoft.goldshop.model.master.JewelryInformation;
import com.kmcsoft.goldshop.model.master.PriceTradingDiamond;
import com.kmcsoft.util.Utilities;
import com.kmcsoft.utils.i18n;
import com.kmcsoft.zkex.PagingListModel;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author kmcsoft02
 */
public class JewelryInformationComposer extends GenericController<JewelryInformation, InformationService> {

    private JewelryInformation parameter;
    private Collection<Currency> currencies;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        currencies=service.getCurrencies();
    }

    
    //<editor-fold defaultstate="collapsed" desc="onCreate">
    public void onCreate() {
        onClick$new(null);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="PagingListModel">
    @Override
    public PagingListModel getListModel() {
        String strFilter = "createdBy.name.matches('(?i).*" + filter.getValue().trim() + ".*')";
        PagingListModel<JewelryInformation> model = Utilities.getPagingListModel(objectClass, HashMap.class, "id,createdBy.name,createdDate", strFilter);
        model.setOrdering("createdDate descending");
        model.initialize();
        return model;
    }

    public void setSelected(HashMap selected) {
        super.setSelected(service.getJewelryInformationById(selected.get("id")));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterNew">
    @Override
    protected void doAfterNew() {
        super.doAfterNew();
        if (getListModel().getSize() != 0) {
            parameter = (JewelryInformation) service.getJewelryInformationById(((HashMap) getListModel().getElementAt(0)).get("id"));
        }
        selected.setUse(true);
        if (parameter != null) {
            for (PriceTradingDiamond ptd : parameter.getDiamondSells()) {
                selected.getDiamondSells().add(new PriceTradingDiamond(ptd.getFromSize(), ptd.getToSize(), ptd.getLostPerPrice(), true));
            }
            for (PriceTradingDiamond ptd : parameter.getDiamondExchanges()) {
                selected.getDiamondExchanges().add(new PriceTradingDiamond(ptd.getFromSize(), ptd.getToSize(), ptd.getLostPerPrice(), false));
            }
            selected.setCurrency(parameter.getCurrency());
            selected.setDescendingByEmployee(parameter.getDescendingByEmployee());
            selected.setDescendingByLeader(parameter.getDescendingByLeader());
            selected.setDiamondWithoutCerfiticate(parameter.getDiamondWithoutCerfiticate());
            selected.setGapPrice(parameter.getGapPrice());
            selected.setItalyGoldExchangeAfterDateLoss(parameter.getItalyGoldExchangeAfterDateLoss());
            selected.setItalyGoldExchangeBeforeDateLoss(parameter.getItalyGoldExchangeBeforeDateLoss());
            selected.setItalyGoldExchangeDate(parameter.getItalyGoldExchangeDate());
            selected.setItalyGoldSellLoss(parameter.getItalyGoldSellLoss());
            selected.setJewelryExchangeLoss(parameter.getJewelryExchangeLoss());
            selected.setJewelrySellLoss(parameter.getJewelrySellLoss());
            selected.setStakesExpiredDate(parameter.getStakesExpiredDate());
            selected.setStakesPerPrice(parameter.getStakesPerPrice());
        }else{
            selected.getDiamondExchanges().add(new PriceTradingDiamond());
            selected.getDiamondSells().add(new PriceTradingDiamond());
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doBeforeSave">
    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        checkBeforeSave();
        if (parameter != null) {
            parameter.setUse(false);
            service.makePersistent(parameter);
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkBeforeSave">
    private void checkBeforeSave() {
        List<PriceTradingDiamond> sellTradingDiamonds = selected.getDiamondSells();
        int size = sellTradingDiamonds.size();
        Collections.sort(sellTradingDiamonds,
                new Comparator<PriceTradingDiamond>() {

                    public int compare(PriceTradingDiamond o1, PriceTradingDiamond o2) {
                        return o2.compareTo(o1);
                    }
                });
        List<PriceTradingDiamond> exchTradingDiamonds = selected.getDiamondExchanges();
        int s = exchTradingDiamonds.size();
        Collections.sort(exchTradingDiamonds,
                new Comparator<PriceTradingDiamond>() {

                    public int compare(PriceTradingDiamond o1, PriceTradingDiamond o2) {
                        return o2.compareTo(o1);
                    }
                });
        for (int i = 1; i < size; i++) {
            if (sellTradingDiamonds.get(i).getFromSize() != 0) {
                if (sellTradingDiamonds.get(i).getFromSize() < (sellTradingDiamonds.get(i - 1).getToSize())) {
                    throw new WrongValueException(i18n.l("shop.parameter.error.sellDiamond.duplicateSize"));
                }
            }
        }
        for (int i = 1; i < s; i++) {
            if (exchTradingDiamonds.get(i).getFromSize() != 0) {
                if (exchTradingDiamonds.get(i).getFromSize() < (exchTradingDiamonds.get(i - 1).getToSize())) {
                    throw new WrongValueException(i18n.l("shop.parameter.error.exchDiamond.duplicateSize"));
                }
            }
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="get/set">
    
    public Collection<Currency> getCurrencies(){
        return currencies;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onInsertRow">
    public void onInsertRow(ForwardEvent event) {
        Grid grid = ((Grid) event.getOrigin().getTarget().getParent().getParent().getParent().getParent());
        if (grid.getId().equalsIgnoreCase("diamondSellGrid")) {
            selected.getDiamondSells().add(new PriceTradingDiamond());
        } else if (grid.getId().equalsIgnoreCase("diamondExchGrid")) {
            selected.getDiamondExchanges().add(new PriceTradingDiamond());
        }
        getBinder().loadComponent(grid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onRemove">
    public void onRemove(ForwardEvent event) {
        Grid grid = (Grid) event.getOrigin().getTarget().getParent().getParent().getParent();
        if (grid.getModel().getSize() == 1) {
            return;
        }
        Toolbarbutton btton = (Toolbarbutton) event.getOrigin().getTarget();
        Row row = ((Row) btton.getParent());
        Object selectedRecord = row.getValue();
        ((BindingListModelList) grid.getModel()).remove(selectedRecord);
        grid.renderAll();
    }
    //</editor-fold>
}
